package com.auto.autoapp.task

import com.auto.autoapp.constants.Dy
import com.auto.autoapp.step.exchangeVideo
import com.auto.autoapp.step.focusUser
import com.auto.autoapp.step.getUserInfo
import com.auto.autoapp.step.sendPrivateMsg
import com.auto.autoapp.step.smoothScreen
import com.auto.autoapp.step.toDyIndex
import com.auto.autoapp.util.addLog
import com.auto.autoapp.util.clickOrTap
import com.auto.autoapp.util.getNodeById
import com.ven.assists.AssistsCore
import com.ven.assists.AssistsCore.findById
import com.ven.assists.AssistsCore.findByTags
import com.ven.assists.AssistsCore.findByText
import com.ven.assists.stepper.Step
import com.ven.assists.stepper.StepCollector
import com.ven.assists.stepper.StepImpl
import kotlinx.coroutines.delay

class GetUserInfoTask : StepImpl() {
    override fun onImpl(collector: StepCollector) {
        collector.next(1) {
            return@next toDyIndex()
        }.next(2) {
            val userNodeResult = Dy.ID_NICKNAME_AT.getNodeById()
            val userNode = userNodeResult.getOrNull()
            if (userNode == null) {
                addLog("未获取到主页入口")
                val exchangeResult = exchangeVideo()
                if (exchangeResult.isFailure) {
                    return@next Step.none
                }
                return@next Step.repeat
            } else {
                val clickResult = userNode.clickOrTap()
                val userNodeVisible = userNode.isVisibleToUser
                addLog("userNodeVisible:$userNodeVisible")
                if (clickResult.isFailure) {
                    addLog("主页点击失败")
                    return@next Step.repeat
                }
                addLog("准备进入主页")
                delay(1000)
                return@next Step.get(3)
            }
            return@next Step.none
        }.next(3) {

            val userResult = getUserInfo()
            addLog("用户信息：${userResult.getOrNull()}")
            return@next Step.get(4)
        }.next(4) {
            focusUser()
            delay(1000)
            sendPrivateMsg("1")
            delay(1000)
            return@next Step.none
        }
    }
}